import Vue from 'vue'
import MinRequest from '@/common/MinRequest.js'
import HTMLParser from '@/uni_modules/ch-html-parser/js_sdk/index.js'

const minRequest = new MinRequest();

// #ifdef H5
const host_api = 'http://localhost:1046/api'; //主服务器
const host_zhuishushenqi = 'http://localhost:1046/zhuishushenqi'; //主服务器
const host_b = 'http://localhost:1046/b'; //主服务器
const host_xbiquge = 'http://localhost:1046/xbiquge'; //笔趣阁-电脑版
const host_xbiquge_m = 'http://localhost:1046/xbiqugem'; //笔趣阁-手机版
//  #endif

// #ifdef APP-PLUS
const host_api = 'https://api.zhuishushenqi.com'; //主服务器
const host_zhuishushenqi = 'https://m.zhuishushenqi.com'; //主服务器
const host_b = 'https://b.zhuishushenqi.com'; //主服务器
const host_xbiquge = 'https://www.xbiquge.la/'; //笔趣阁
const host_xbiquge_m = 'https://m.xbiquge.la/'; //笔趣阁
//  #endif
let ip = '192.168.12.30'
// 设置默认配置
minRequest.setConfig((config) => {
	config.baseURL = host_api
	config.header = {
		'x-forwarded-for': ip,
		// 'Proxy-Client-IP': ip,
		'WL-Proxy-Client-IP': ip
	}
	// config.timeout = 2000
	return config
})

// 请求拦截器
minRequest.interceptors.request((request) => {
	// console.log('请求拦截器', request)

	return request
})

// 响应拦截器
minRequest.interceptors.response((response) => {
	// console.log('响应拦截器：', response)

	if (response.errMsg === 'request:ok') {
		if (response.statusCode === 200) {
			return response.data
		}
	}
})

export default {
	// 这里统一管理api请求
	apis: {
		async getCategoryData_zssq(params) {
			let res = await minRequest.get(`${host_zhuishushenqi}/category?pageSource=2`, params)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('.category-section')
			let returnData = htmlData.map(item => {
				return {
					genderName: item.children[0].innerText,
					data: item.children[1].children.map(subItem => {
						return {
							href: subItem.href,
							major: subItem.children[0].innerText,
							count: subItem.children[1].innerText,
						}
					})
				}
			})
			return returnData
		},
		async getCategoryData_xbqg(params) {
			let res = await minRequest.get(`${host_xbiquge}/sort/`, params)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('.content .prev')
			let returnData = [{
				genderName: '',
				data: htmlData.map((item, index) => {
					return {
						href: 'fenlei/' + (index + 1),
						major: item.children[0].innerText,
					}
				})
			}]
			return returnData
		},
		async getCategoryLevel_zssq(params) {
			let res = await minRequest.get(`${host_zhuishushenqi}${params}`, params)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('#J_hiddenMinorValue')
			let returnData = htmlData[0].value.split(',')
			returnData.unshift('全部')
			return returnData
		},
		async getBookList_zssq(params) {
			let returnData = false
			let res = await minRequest.get(`${host_b}/books/fuzzy-search`, params)
			if (res.ok) {
				let t_books = res.books.map(item => {
					item.cover = decodeURIComponent(item.cover).replace('/agent/', '')
					item.wordCount = Vue.prototype.$formatWordcount(item.wordCount)
					return item
				})
				let t_pages = Math.ceil(res.total / params.limit)
				returnData = {
					books: t_books,
					pages: t_pages
				}
			}
			return returnData
		},
		async getBookList_xbqg(params) {
			let res = await minRequest.get(`${host_xbiquge}/${params}`, params)
			let htmlparser = new HTMLParser(res)
			let bookData = htmlparser.find('#newscontent .l ul li')
			let pagesData = htmlparser.find('#pagestats')
			let t_books = bookData.map(item => {
				let href = item.children[0].children[0].href
				let hreflist = href.split('/')
				return {
					_id: hreflist[hreflist.length - 2],
					title: item.children[0].children[0].innerText,
					lastChapter: item.children[1].children[0].innerText,
					author: item.children[2].innerText
				}
			})
			let t_pages = pagesData[0].innerText.split('/').map(item => parseInt(item))[1]
			let returnData = {
				books: t_books,
				pages: t_pages
			}
			return returnData
		},
		async getBookDetail_zssq(params) {
			let returnData = false
			let res = await minRequest.get(`${host_api}/book/${params}`)
			if (res) {
				returnData = JSON.parse(JSON.stringify(res))
				returnData.cover = decodeURIComponent(returnData.cover).replace('/agent/', '')
				returnData.wordCount = Vue.prototype.$formatWordcount(returnData.wordCount)
				returnData.updated = Vue.prototype.$formatTimestamp(new Date(returnData.updated))
			}
			return returnData
		},
		async getBookDetail_xbqg(params) {
			let res = await minRequest.get(`${host_xbiquge}/${params.substr(0,2)}/${params}/`)
			let htmlparser = new HTMLParser(res)
			let cat = htmlparser.find('.con_top a')
			let title = htmlparser.find('#info h1')
			let info = htmlparser.find('#info p')
			let intro = htmlparser.find('#intro p')
			let cover = htmlparser.find('#fmimg img')
			let returnData = {
				_id: params,
				title: title[0].innerText,
				author: info[0].innerText.split('：')[1],
				updated: info[2].innerText.split('：')[1],
				lastChapter: info[3].children[0].innerText,
				cat: cat[2].innerText,
				longIntro: intro[1].innerText,
				cover: cover[0].src,
			}
			return returnData
		},
		async getChapterList_zssq(params) {
			let res = await minRequest.get(`${host_zhuishushenqi}/book/${params}/chapter`)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('#J_chapterListUp li')
			let returnData = htmlData.map(item => {
				return {
					id: params,
					index: item.children[0]['data-index'],
					title: item.children[0]['data-title']
				}
			})
			return returnData
		},
		async getChapterList_xbqg(params) {
			let res = await minRequest.get(`${host_xbiquge}/${params.substr(0,2)}/${params}/`)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('#list dl dd a')
			let returnData = htmlData.map(item => {
				return {
					id: params,
					index: item.href.replace('.html', '').split('/')[3],
					title: item.innerText
				}
			})
			return returnData
		},
		async getBookByKey_zssq(params) {
			let returnData = false
			let res = await minRequest.get(`${host_api}/book/fuzzy-search`, params)
			if (res.ok) {
				returnData = res.books.map(item => {
					item.cover = decodeURIComponent(item.cover).replace('/agent/', '')
					item.wordCount = Vue.prototype.$formatWordcount(item.wordCount)
					return item
				})
			}
			return returnData
		},
		async getBookByKey_xbqg(params) {
			let res = await minRequest.get(`${host_xbiquge}/modules/article/waps.php`, params)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('.grid tr')
			let returnData = htmlData.filter(item => !item.align).map(item => {
				let href = item.children[0].children[0].href
				let hreflist = href.split('/')
				return {
					_id: hreflist[hreflist.length - 2],
					title: item.children[0].children[0].innerText,
					lastChapter: item.children[1].children[0].innerText,
					author: item.children[2].innerText,
				}
			})
			return returnData
		},
		async getBookContent_zssq(params) {
			let res = await minRequest.get(`${host_zhuishushenqi}/book/${params.id}/${params.index}`)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('#J_readerContent .inner p')
			let returnData = htmlData.filter(item => item.innerText)
				.map(item => item.innerText)
				.join('<br />')
			return returnData
		},
		async getBookContent_xbqg(params) {
			let path = `${params.id.substr(0,2)}/${params.id}/${params.index}.html`
			let res = await minRequest.get(`${host_xbiquge}/${path}`)
			let htmlparser = new HTMLParser(res)
			let htmlData = htmlparser.find('#content br')
			let returnData = htmlData.filter(item => item.outerText)
				.map(item => item.outerText)
				.join('<br />')
			return returnData
		},
	},
}
